import time

import tkinter as tk
from tkinter import ttk
from random import *
class Splash:

    def __init__(self, root, file, wait):
        self.__root = root
        self.__file = file
        self.__wait = wait + time.clock()
        self.__blagounette = tk.StringVar()
        self.__tas_de_blagounette = ["Calcul de Pi", "Installation de l'épisode 3 de Half-Life",
									"Traduction des animés", "Téléchargement de Wikipédia",
									"Redémarrage de l'ordinateur", "Chargement de la météo",
									"Launching sheep to the moon", "All your base are belong to us",
									"Envoi de vos données personnelles", "Analyse en profondeur de votre disque dur",
									"Déphasage des protons", "Mise à jour de la recette du Kebab",
                                    "Desu desu~", "Envoi de vos coordonnées bancaires",
                                    "Alignement des étoiles", "Rangement de la chambre",
									"Division par zéro", "Ouverture d'une fenêtre d'hyperespace"]

    def __enter__(self):
        # Hide the root while it is built.
        self.__root.withdraw()
        # Create components of splash screen.
        window = tk.Toplevel(self.__root)
        canvas = tk.Canvas(window)
        splash = tk.PhotoImage(master=window, file=self.__file)
        waiter = ttk.Label(master=window,textvariable = self.__blagounette, anchor=tk.N)
        # Get the screen's width and height.
        scrW = window.winfo_screenwidth()
        scrH = window.winfo_screenheight()
        # Get the images's width and height.
        imgW = splash.width()
        imgH = splash.height()
        # Compute positioning for splash screen.
        Xpos = (scrW - imgW) // 2
        Ypos = (scrH - imgH) // 2
        # Configure the window showing the logo.
        window.overrideredirect(True)
        window.geometry('+{}+{}'.format(Xpos, Ypos))
        # Setup canvas on which image is drawn.
        canvas.configure(width=imgW, height=imgH, highlightthickness=0)
        canvas.pack(expand=True)
        waiter.pack(side = tk.BOTTOM,fill='x')
        self.__blagounette.set(choice(self.__tas_de_blagounette)+"...")
        # Show the splash screen on the monitor.
        canvas.create_image(imgW // 2, imgH // 2, image=splash)
        window.update()
        # Save the variables for later cleanup.
        self.__window = window
        self.__canvas = canvas
        self.__splash = splash
        self.__waiter = waiter
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        # Ensure that required time has passed.
        now = time.clock()
        if now < self.__wait:
            time.sleep(self.__wait - now)
        # Free used resources in reverse order.
        del self.__splash
        self.__canvas.destroy()
        self.__window.destroy()
        self.__waiter.destroy()
        # Give control back to the root program.
        self.__root.update_idletasks()
        self.__root.deiconify()

    def step(self):
        self.__blagounette.set(choice(self.__tas_de_blagounette)+"...")
        self.__window.update()
